package com.learn.egcar1.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.learn.egcar1.base.response.JsonResult;
import com.learn.egcar1.base.response.StatusCode;
import com.learn.egcar1.mapper.VehicleMapper;
import com.learn.egcar1.pojo.dto.VehicleAddParam;
import com.learn.egcar1.pojo.dto.VehicleListQuery;
import com.learn.egcar1.pojo.entity.Vehicle;
import com.learn.egcar1.pojo.vo.UserVO;
import com.learn.egcar1.pojo.vo.VehicleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Validated
@RestController
@Slf4j
@Api(tags = "车辆管理模块")
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @ApiOperation("01.添加车辆信息")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam,@ApiIgnore HttpSession session){
        log.debug("vehicleAddParam:" +vehicleAddParam);
        //校验登录状态
        UserVO userVO = (UserVO) session.getAttribute("user");
        /*if(userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/

        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setUserId(userVO.getId());
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());

        log.debug("vehicle:"+vehicle);
        vehicleMapper.addVehicle(vehicle);

        return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation("02.获取车辆列表")
    @ApiOperationSupport(order = 20)////////////////////////这里有问题，get的请求不需要@RequBody因为不是json格式，细心点
    public JsonResult getVehicleList(/*@RequestBody */VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session){
        log.debug("vehicleListQery:" + vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
        /*if(userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/
        log.debug("userVO:" + userVO);

        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userId = userVO.getId();
        log.debug("brand:" + brand + ",license:" + license + ",userId:" + userId);
        List<VehicleVO> vehicleList = vehicleMapper.getVehicleList(brand, license, userId);
        log.debug("vehicleList:" + vehicleList);

        return JsonResult.ok(vehicleList);
    }

    @PostMapping("del/{id}")
    @ApiOperation("03.删除车辆信息")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "车辆ID", required = true, dataType = "Long", paramType = "path")
    public JsonResult deleteVehicle(@Range(min = 1,max = 100,message = "车辆id必须在1到100之间") @PathVariable Long id, @ApiIgnore HttpSession session){
        //手动抛出异常
        if(id < 0){
            throw new IllegalArgumentException("id不能小于0");
        }

        log.debug("id:" + id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        /*if(userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/

        Long userId = userVO.getId();
        int res = vehicleMapper.deleteVehicle(id, userId);
        log.debug(res>0?"删除成功":"删除失败");

        return JsonResult.ok();
    }
}
